package com.mh.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mh.shiro.business.entity.Role;
import com.mh.shiro.business.service.SysRoleService;
import com.mh.shiro.business.vo.RoleConditionVO;
import com.mh.shiro.framework.exception.MhException;
import com.mh.shiro.persistence.beans.SysRole;
import com.mh.shiro.persistence.mapper.SysRoleMapper;
import com.mh.shiro.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年06月21日17:26
 * @Description: 角色
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 获取ztree使用的角色列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> queryRoleListWithSelected(Integer userId) {
        List<SysRole> sysRoles = roleMapper.queryRoleListWithSelected(userId);

        if (CollectionUtils.isEmpty(sysRoles)) {
            return null;
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = null;
        for (SysRole role : sysRoles) {
            map = new HashMap<>(3);
            map.put("id", role.getId());
            map.put("pId", 0);
            map.put("checked", role.getSelected());
            map.put("name", role.getDescription());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public PageInfo<Role> findPageBreakByCondition(RoleConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysRole> sysRoles = roleMapper.findPageBreakByCondition(vo);

        if (CollectionUtils.isEmpty(sysRoles)) {
            return null;
        }
        List<Role> roles = new ArrayList<>();
        for (SysRole r : sysRoles) {
            roles.add(new Role(r));
        }
        PageInfo bean = new PageInfo<SysRole>(sysRoles);

        bean.setList(roles);

        return bean;
    }

    /**
     * 获取用户的角色
     * @param userId
     * @return
     */
    @Override
    public List<Role> listRolesByUserId(Long userId) {
        List<SysRole> sysRoles = roleMapper.listRolesByUserId(userId);
        if(CollectionUtils.isEmpty(sysRoles)) {
            return null;
        }
        List<Role> roles = new ArrayList<>();
        for(SysRole r : sysRoles) {
            roles.add(new Role(r));
        }
        return roles;
    }

    @Override
    public Role insert(Role entity) {
        Assert.notNull(entity, "Role不可为空！");
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        roleMapper.insert(entity.getSysRole());

        return entity;
    }

    @Override
    public void insertList(List<Role> entities) {

    }

    @Override
    public boolean removeByPrimaryKey(Long primaryKey) {
        return roleMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    public boolean update(Role entity) {
        return false;
    }

    @Override
    public boolean updateSelective(Role role) {
        Assert.notNull(role, "Role不可为空");
        role.setUpdateTime(new Date());
        System.out.println(roleMapper.updateByPrimaryKeySelective(role.getSysRole()));

        return true;
    }

    @Override
    public Role getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空");
        SysRole role = roleMapper.selectByPrimaryKey(primaryKey);

        return null == role ? null : new Role(role);
    }

    @Override
    public Role getOneByEntity(Role entity) {
        return null;
    }

    @Override
    public List<Role> listAll() {
        return null;
    }

    @Override
    public List<Role> listByEntity(Role entity) {
        return null;
    }
}
